﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class WorkflowControlRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "WorkflowControlRenderBll";
            }
        }

        public override string Title
        {
            get
            {
                return "Thiết lập quy trình cho nghiệp vụ";
            }
        }



        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                string UserType = (string)WebEnvironments.Request(SiteId, "type");
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(UserType)) UserType = "";
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientSave()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Max = parseInt(document.getElementById('txtMax').value);\r\n"+
                   "        SaveData='';\r\n"+
                   "        for(var iIndex=0;iIndex<Max;iIndex++)\r\n"+
                   "        {\r\n"+
                   "            Id = document.getElementById('txtId'+iIndex).value;\r\n"+
                   "            WorkflowControlId = document.getElementById('drpSelectWorkflowDef'+Id).value;\r\n"+
                   "            SaveData=SaveData+Id+'~'+WorkflowControlId+';'\r\n"+
                   "        }\r\n"+
                   "        AjaxOut = Portal.Systems.Render.WorkflowControlRenderBll.ServerSideSave(SiteId, SaveData).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<div id=divActionForm style=\"display:none;\"></div>\r\n" +
                   "<input type=hidden id=txtUserType value=\"" + UserType + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSave();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại thiết lập</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div id=\"divListWorkflowControl\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId).HtmlContent +
                   "  </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(string SiteId, string SaveData)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] Items = SaveData.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                WorkflowControlCls[]
                    WorkflowControls = new WorkflowControlCls[Items.Length];
                for (int iIndex = 0; iIndex < WorkflowControls.Length; iIndex++)
                {
                    string[] Ids=Items[iIndex].Split(new char[]{'~'},StringSplitOptions.RemoveEmptyEntries);
                    WorkflowControls[iIndex] = new WorkflowControlCls();
                    WorkflowControls[iIndex].WorkflowControlId = Ids[0];
                    if (Ids.Length > 1)
                    {
                        WorkflowControls[iIndex].WorkflowDefId = Ids[1];
                    }
                    else
                    {
                        WorkflowControls[iIndex].WorkflowDefId = null;
                    }
                }
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveWorkflowControls(SiteId, WorkflowControls);
                OAjaxOut.InfoMessage = "Cập nhật thành công";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                WorkflowControlCls[] WorkflowControls = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingWorkflowControls(SiteId);
                WorkflowDefCls[] WorkflowDefs = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingWorkflowDef(SiteId);
                string Subject = "Danh sách quy trình trên cổng";
                string Html =
                   " <input type=hidden id=txtMax value=\""+WorkflowControls.Length+"\">\r\n"+
                   "        <h3>" + Subject + "</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th>Quy trình</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < WorkflowControls.Length; iIndex++)
                {
                    string SelectWorkflowDef =
                        "<select id=\"drpSelectWorkflowDef" + WorkflowControls[iIndex].WorkflowControlId + "\">\r\n" +
                        "   <option value=\"\">Chưa xác định quy trình</option>\r\n";
                    for (int iIndexWf = 0; iIndexWf < WorkflowDefs.Length; iIndexWf++)
                    {
                        if (WorkflowDefs[iIndexWf].WorkflowDefId.Equals(WorkflowControls[iIndex].WorkflowDefId))
                        {
                            SelectWorkflowDef += "    <option selected value=\"" + WorkflowDefs[iIndexWf].WorkflowDefId + "\">" + WorkflowDefs[iIndexWf].WorkFlowName + "</option>\r\n";
                        }
                        else
                        {
                            SelectWorkflowDef += "    <option value=\"" + WorkflowDefs[iIndexWf].WorkflowDefId + "\">" + WorkflowDefs[iIndexWf].WorkFlowName + "</option>\r\n";
                        }
                    }
                    SelectWorkflowDef += "</select>\r\n";
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "<input type=hidden id=\"txtId" + iIndex + "\" value=\"" + WorkflowControls[iIndex].WorkflowControlId + "\"></td>\r\n" +
                       "        <td class=tdCode>" + WorkflowControls[iIndex].Title + "</td>\r\n" +
                       "        <td>"+SelectWorkflowDef+"</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
